package javaTest02.utils;

import sun.misc.ProxyGenerator;

import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;

public class LogMethodInvoke {
    public static void log(String methodName,String[] args,String path) {

        BufferedWriter writer=null;
        try {
            File file=new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            //拼接日志字符串
            StringBuilder builder=new StringBuilder();
            builder.append("调用方法:").append(methodName).append("\t");

            builder.append("参数列表:");
            for (String arg : args)
                builder.append(arg);
            builder.append("\t");

            builder.append("调用时间:").append(LocalDateTime.now());
            String line=builder.toString();

            writer=new BufferedWriter(new FileWriter(path,true));
            writer.write(line,0,line.length());
            writer.newLine();//换行

        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (writer!=null){
                try {
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
    public static void generateProxyClassFile(String className,Class<?> clazz){//生成委托类的代理类对应的.class文件
        try {
            byte[] ClassFile = ProxyGenerator.generateProxyClass(className, clazz.getInterfaces());
            File file=new File("src/javaTest02/exercise_3/out/"+className+".class");
            if (!file.exists()) {
                file.createNewFile();
            }

            FileOutputStream out=new FileOutputStream(file);
            out.write(ClassFile);
            out.flush();
            out.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
